package fr.afcepf.vod.dao;

import java.util.Date;
import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import fr.afcepf.vod.entities.Client;
import fr.afcepf.vod.entities.Description;
import fr.afcepf.vod.entities.Tarifvideounite;
import fr.afcepf.vod.entities.Video;
@Named
@Transactional
public class DaoVideoImpl implements IDaoVideo {
	
	@PersistenceContext
	private EntityManager em;
	
	
	@Override
	public void ajouterVideo(Video video) {

		em.persist(video);

	}

	@Override
	public void relierVideoEtTarif(Video video, Tarifvideounite tarif) {
		//em.createQuery("").getResultList();
		Video videoBase = (Video)em.find(Video.class, video.getIdVideo());
		Tarifvideounite tarifBase = (Tarifvideounite)em.find(Tarifvideounite.class, tarif.getIdTarif());
		
//		videoBase.getTarifvideounites2().add(tarifBase);
		tarifBase.getVideos1().add(videoBase);
		
//		em.merge(tarifBase);
//		em.merge(videoBase);
	}
	
	@Override
	public List<Video> rechercherVideo(String intituleRecherche){
		
		//Query query = em.createQuery("from Video v where v.nom like %:"+intituleRecherche+"%");
		Query query = em.createQuery("from Video v where v.nom like :intitule");
		query.setParameter("intitule", "%"+intituleRecherche+"%");
		List<Video> video = (List<Video>)query.getResultList();
		
		return video;
	}
	
	@Override
	public List<Video> listerVideos(){
		List<Video> laListe;
		
		Query query = em.createQuery("Select v from Video as v");
		
		laListe =  (List<Video>)query.getResultList();
		for (Video v:laListe){
			v.getDescriptions().size();
		}
		return laListe;
			
	}

	@Override
	public Tarifvideounite getTarifVideo(int idVideo) {
		
//		Video video = em.find(Video.class, idVideo);
		Query query = em.createQuery("Select t from Tarifvideounite t inner join fetch t.videos1 v where v.idVideo = :idVideo");
		query.setParameter("idVideo", idVideo);
		List<Tarifvideounite> listeResultat = query.getResultList();
		
		System.out.println(listeResultat.get(0).getIdTarif());
		
		return listeResultat.get(0);
	}

	@Override
	public void associerDescriptionVideo(Video video, Description d) {
		Video videoBase = (Video)em.find(Video.class, video.getIdVideo());
		Description descrBase=(Description)em.find(Description.class, d.getIdDescription());
		videoBase.getDescriptions().add(descrBase);
	}
	
	@Override
	public void dissocierDescriptionVideo(Video video, Description d) {
		Video videoBase = (Video)em.find(Video.class, video.getIdVideo());
		Description descrBase=(Description)em.find(Description.class, d.getIdDescription());
		videoBase.getDescriptions().remove(descrBase);
	}

	@Override
	public Video GetVideoById (int idVideo){
		Query query = em.createQuery("from Video v where v.idVideo = :idVideo");
		query.setParameter("idVideo", idVideo);
		List<Video> listeResultat = query.getResultList();
		if (listeResultat.size()>0){
			Video maVideo = (Video) listeResultat.get(0);
			maVideo.getDescriptions().size();
			return maVideo;
		}
		return null;
		
	}

	
}
